﻿using System;
using System.Linq;
using System.Text;

namespace Infinity.Common
{
	/// <summary>
	/// Manager class for exception handling
	/// </summary>
	public class BaseExceptionManager
	{
		public event Action<BaseExceptionManager, Exception> ExceptionRaised;

		public virtual void Do(Delegate action)
		{
			Do(action, null);
		}

		public virtual void Do(Delegate action, Delegate finallyAction)
		{
			Ensure.NotNull("action", action);
			try
			{
				action.DynamicInvoke();
			}
			catch (Exception ex)
			{
				OnExceptionRaised(this, ex);
			}
			finally
			{
				if (finallyAction != null)
					finallyAction.DynamicInvoke();
			}
		}

		protected virtual void OnExceptionRaised(BaseExceptionManager sender, Exception exception)
		{
			if (ExceptionRaised != null)
				ExceptionRaised(sender, exception);
		}
	}
}
